<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
import WebSocket from "@/views/login/web-socket";
import Event from "@/utils/event-bus";
import {Notification} from "element-ui";

export default {
  name: 'App',
  created() {
    if (sessionStorage.getItem("store")) {
      this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem("store"))))
      const id = this.$store.state.user.shop.id
      if (id) {
        WebSocket.init(id, (data) => {
          Event.$emit('reflush_order');
          data = JSON.parse(data)
          Notification({
            title: '您有新的订单！',
            type: 'success',
            duration: 0,
            message: `${data.locationId} 号桌有新的订单`
          });
        })
      }
    }

    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      console.log("beforeunload");
      sessionStorage.setItem("store", JSON.stringify(this.$store.state))
    })

  }
}
</script>
